package SeparateAnalysis;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by IntelliJ IDEA.
 * User: Mike
 * Date: 10/29/11
 * Time: 6:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class Writer {

    static void WriteData(boolean writeOut, Double runningTime) throws IOException {        //Write out Files ================================================================
        if (writeOut) {
            System.out.println("Thermocouple = " + Data.tempTC);
            String Dir = "C:\\WhiskeyData\\output.txt";
            DecimalFormat df1 = new DecimalFormat("###.##");
            FileWriter write = new FileWriter(Dir, true);
            PrintWriter text = new PrintWriter(write, true);
            File txtFile = new File("C:\\WhiskeyData\\output.txt");
            if (!txtFile.exists()) {
                String output = "datetime"
                        + "\t" + "milliTime"
                        + "\t" + "min"
                        + "\t" + "columnC"
                        + "\t" + "columnF"
                        + "\t" + "ambientC"
                        + "\t" + "ambientF"
                        + "\t" + "bottleNumber"
                        + "\t" + "correctedAbv"
                        + "\t" + "vaporAbv"
                        + "\t" + "distillateTempC"
                        + "\t" + "boilerTempC"
                        + "\t" + "flowRate"
                        ;
                System.out.println(output);
                text.println(output);
                text.flush();
                write.close();
                text.close();
                write = null;
                text = null;
            }
            String output = CurrentTime()                        // 0
                    + "\t" + Data.milliTime              // 1
                    + "\t" + df1.format(runningTime)             // 2
                    + "\t" + Data.tempTC                         // 3
                    + "\t" + Data.tempTF                         // 4
                    + "\t" + Data.tempAC                         // 5
                    + "\t" + Data.tempAF                         // 6
                    + "\t" + Data.bottleS                        // 7
                    + "\t" + Data.corrAbvS                       // 8
                    + "\t" + Data.vaporAbvS                      // 9
                    + "\t" + Data.distTemp                       // 10
                    + "\t" + Data.boilTempS                      // 11
                    + "\t" + Data.flowRate                       // 12
                    ;
            System.out.println(output);
            text.println(output);
            text.flush();
            write.close();
            text.close();
            write = null;
            text = null;
            Data.numOfWrites++;
        }
    }

    public static String CurrentTime() {
        Calendar calendar = new GregorianCalendar();

        String am_pm;
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if (calendar.get(Calendar.AM_PM) == 0)
            am_pm = "AM";
        else {
            hour = hour + 12;
            am_pm = "PM";
        }
        month++;
        Data.dateTimeS = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

        return Data.dateTimeS;
    }

}
